<?php
namespace app\common\model\shop\ozon;

use think\admin\Model;
use think\facade\Cache;

/**
 * Undocumented class
 * @package app\common\model\shop
 * @author xuli
 * @DateTime 2024-10-25 10:49
 */
class OzonGoodsCategoryModel extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'shop_goods_category_ozon';

    // 自动写入时间戳
    protected $autoWriteTimestamp = false;

    /**
     * 日志名称
     * @var string
     */
    protected $oplogName = 'Ozon分类';

    /**
     * 日志类型
     * @var string
     */
    protected $oplogType = 'Ozon分类管理';

    /**
     * @param $array
     * @param $is_expand
     * @param $parentId
     * @return array
     */
    public static function categoryTree($array, $is_expand = 0, $parentId = 0) {
        if (empty($array)) {
            return [];
        }
        $tree = array();
        foreach($array as $item) {
            if ($item['pid'] == $parentId) {
                $tree[] = array(
                    'id'    => $item['type_id'],
                    'title'  => $item['title'],
                    'children' => self::categoryTree($array, $is_expand, $item['type_id']),
                    'spread' => $is_expand == 1 ? true : false, // 是否展开
                );
            }
        }
        Cache::set('shop_ozon_category_tree', $tree, 86400 * 30);
        return $tree;
    }

    public static function getCategoryTree()
    {
        $tree = Cache::get('shop_ozon_category_tree');
        if(empty($tree)) {
            return [];
        }
        return $tree;
    }


}